package custormconfig

import (
	"github.com/Unknwon/goconfig"
	"socketTokafka/logger"
)

/**
[udp]
address=0.0.0.0
port=7777
[kafka]
broker=10.65.66.181:9092
topicA=test1_socket
topicB=test_heka
[goroutine]
kafkaLimitedChan=100
limitChan=2000
msgChanLimit=2000
*/

type KafkaConfig struct {
	TopicA string
	TopicB string
	TopicC string
	Broker string
}

type UdpConfig struct {
	UdpAddress string
	UdpPort    string
}
type TheadConfig struct {
	LimitChan        int // 限制goroutine数量
	KafkaLimitedChan int
	MsgChanLimit     int // 限制同时处理消息数量
}

var cfg *goconfig.ConfigFile

func init() {
	var err error
	cfg, err = goconfig.LoadConfigFile("conf.ini")
	if err != nil {
		logger.Logger.Error("配置文件读取失败，错误信息：{}", err)
	}
}

func InitKafkaConfig() *KafkaConfig {
	broker, err := cfg.GetValue("kafka", "broker")
	if err != nil {
		logger.Logger.Error("kafka broker 参数读取失败")
	}
	topicA, err := cfg.GetValue("kafka", "topicA")
	if err != nil {
		logger.Logger.Error("kafka topicA 参数读取失败")
	}
	topicC, err := cfg.GetValue("kafka", "topicC")
	if err != nil {
		logger.Logger.Error("kafka topicC 参数读取失败")
	}
	topicB, err := cfg.GetValue("kafka", "topicB")
	if err != nil {
		logger.Logger.Error("kafka topicB 参数读取失败")
	}
	return &KafkaConfig{
		TopicA: topicA,
		TopicB: topicB,
		TopicC: topicC,
		Broker: broker,
	}
}

func InitUdpConfig() *UdpConfig {
	address, err := cfg.GetValue("udp", "address")
	if err != nil {
		logger.Logger.Error("udp address 参数读取失败")
	}
	port, err := cfg.GetValue("udp", "port")
	if err != nil {
		logger.Logger.Error("udp port 参数读取失败")
	}
	return &UdpConfig{UdpAddress: address, UdpPort: port}
}

func InitThreadConfig() *TheadConfig {
	kafkaLimitedChan, err := cfg.Int("goroutine", "kafkaLimitedChan")
	if err != nil {
		logger.Logger.Error("kafka goroutineNumber 参数读取失败")
	}
	limitChan, err := cfg.Int("goroutine", "limitChan")
	if err != nil {
		logger.Logger.Error("kafka goroutineNumber 参数读取失败")
	}
	msgChanLimit, err := cfg.Int("goroutine", "msgChanLimit")
	if err != nil {
		logger.Logger.Error("kafka goroutineNumber 参数读取失败")
	}
	return &TheadConfig{
		KafkaLimitedChan: kafkaLimitedChan,
		LimitChan:        limitChan,
		MsgChanLimit:     msgChanLimit,
	}
}
